package com.example.studentregistration.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.studentregistration.mapper.ClazzMapper;
import com.example.studentregistration.pojo.Clazz;
import com.example.studentregistration.service.ClazzService;
import com.example.studentregistration.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


import java.net.URLEncoder;


import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public Result<?> insertClazz(Clazz clazz) {
        // 班级编号唯一校验
        Clazz exist = clazzMapper.queryClazzById(clazz.getId());
        if (exist != null) {
            return Result.fail("班级编号已存在");
        }
        int r = clazzMapper.insertClazz(clazz);
        return r > 0 ? Result.success("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result<?> updateClazz(Clazz clazz) {
        int r = clazzMapper.updateClazz(clazz);
        return r > 0 ? Result.success("修改成功") : Result.fail("修改失败");
    }

    @Override
    public Result<?> deleteClazz(Integer id) {
        int r = clazzMapper.deleteClazz(id);
        return r > 0 ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Page<Clazz> queryClazzPage(Integer currentPage, Integer pageSize,
                                      Integer grade, String className) {
        PageHelper.startPage(currentPage, pageSize);
        List<Clazz> list = clazzMapper.queryClazzByPage(grade, className);
        return (Page<Clazz>) list;
    }

    @Override
    public Result<Clazz> queryClazzById(Integer id) {
        Clazz c = clazzMapper.queryClazzById(id);
        return c != null ? Result.success(c) : Result.fail("班级不存在");
    }

    @Override
    public Result<?> batchDeleteClazz(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("未选中任何班级");
        }
        int r = clazzMapper.batchDeleteClazz(ids);
        return r > 0 ? Result.success("批量删除成功") : Result.fail("批量删除失败");
    }

    @Override
    public void exportClazzExcel(HttpServletResponse response) throws IOException {
        List<Clazz> list = clazzMapper.queryAllClazz();   // ① 查数据
        System.out.println(">>> 裸写条数：" + list.size());

        ExcelWriter writer = ExcelUtil.getWriter();       // ② 创建 writer
        writer.write(list, true);                         // ③ 直接写，不设别名

        // ④ 输出到浏览器
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("班级信息_裸写", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);   // true=关闭流
        writer.close();
    }

    @Override
    public Result<?> queryClazzByClassName(String className) {
        Clazz clazz = new Clazz();
            clazz= clazzMapper.queryClazzByClassName( className);
            return  Result.success(clazz);


    }


}
